/*
 * @Author: szx
 * @Date: 2022-04-09 14:55:04
 * @LastEditTime: 2022-04-09 15:02:23
 * @Description:
 * @FilePath: \leetcode\400-499\496\496_2.js
 */
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var nextGreaterElement = function (nums1, nums2) {
    const map = new Map();
    nums1.forEach((v, i) => {
        map.set(v, i);
    });
    const st = [nums2[0]];
    const res = new Array(nums1.length).fill(-1);
    for (let i = 1; i < nums2.length; i++) {
        while (st.length && st[st.length - 1] < nums2[i]) {
            const top = st[st.length - 1];
            res[map.get(top)] = top;
            st.pop();
        }
        st.push(nums2[i]);
    }
    return res;
};
